package com.serkol.ams.module.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.serkol.ams.module.sys.entity.SysMenu;
import com.serkol.ams.module.sys.entity.SysUsers;
import com.serkol.ams.module.sys.service.SysMenuService;
import com.serkol.ams.module.sys.valid.SysMenuParams;
import com.serkol.ams.utils.MenuTree;
import com.serkol.ams.utils.R;
import com.serkol.ams.vo.Result;
import com.serkol.ams.vo.RouterVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 菜单表
 *
 * @author zlj
 * @email 328727561@qq.com
 * @date 2024-04-17 11:28:08
 */
@Tag(name = "菜单表")
@RestController
@RequestMapping("sys/menu")
public class MenuController {
    @Resource
    private SysMenuService permissionService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list() {
        List<SysMenu> data = permissionService.findyTreeList();

        return Result.success(data);
    }

    /**
     * 获取菜单列表数据
     */
    @GetMapping("/navList")
    public Result navList() {
        //从spring Security上下文获取到用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //判断authentication对象是否为空
        if (authentication == null) {
            return Result.failure("用户信息查询失败!");
        }
        //获取用户信息
        SysUsers user = (SysUsers) authentication.getPrincipal();
        System.out.println("user = " + user);
        System.out.println("user.getPermissionList() = " + user.getPermissionList());
        //用户权限集合
        List<SysMenu> permissionList = user.getPermissionList();
        //筛选当前用户权限拥有的菜单数据
        List<SysMenu> collect = permissionList.stream()
                // 0 目录 1菜单
                //只筛选目录与菜单数据，按钮不需要加到路由中
                .filter(e -> e != null && e.getType() != 2).collect(Collectors.toList());
        //生成路由数据
        List<RouterVo> routerVos = MenuTree.makeRouter(collect, 0L);
        //  return R.ok("菜单数据获取成功").put("data", routerVos);
        return Result.success(routerVos);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SysMenu permission = permissionService.getById(id);

        return R.ok().put("permission", permission);
    }

    /**
     * 保存
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody SysMenuParams data) {
        SysMenu permission = new SysMenu();
        BeanUtil.copyProperties(data, permission);
        System.out.println("permission = " + permission);
        boolean flag = false;
        if (permission.getId() != null) {
            if (permission.getId() > 0) {
                flag = permissionService.updateById(permission);
            } else {
                flag = permissionService.save(permission);
            }
        } else {
            flag = permissionService.save(permission);
        }


        return flag ? Result.success() : Result.failure();
    }


}
